<?php
/*
 * @Author: CHINAKAIYUAN<sunkaiyuan@namenode.cn>
 * @Date: 2022-02-02 19:42:06
 * @LastEditTime: 2023-05-06 00:41:32
 * @LastEditors: 孙开源 && sunkaiyuan@namenode.cn
 * @Description: 
 */

namespace App\Repositories;

use App\Models\Attachment;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Storage;
use Intervention\Image\Facades\Image;

class AttachmentRepository extends Repositories
    {
    protected $relation_models = ['creator'];

    public function boot(Attachment $model)
        {
        $this->model = $model;
        }
    public function upload()
        {

        $file = request()->file;
        // Image::class;
        $path = $this->getPath();
        $name = $this->getName($file);

        // dd($file->getClientOriginalName());
        $attModel = $this->model->create([
            'name'          => $file->getClientOriginalName(),
            'filetype'      => $file->getMimeType(),
            'fileext'       => $file->getClientOriginalExtension(),
            'filepath'      => "/" . $path . "/" . $name,
            'filethumbnail' => "/" . $path . "/" . $name,
            'filesize'      => $file->getSize(),
            'uuid'          => uuid()
        ]);

        if ($this->isImage($file)) {
            //缩略图
            $filethumbnail = "/" . $path . "/thumbnail_" . $name;

            Storage::put(
                $filethumbnail,
                Image::make($file)->resize(100, 100)->encode($attModel->fileext)
            );

            $attModel->filethumbnail = $filethumbnail;
            }

        $file->storeAs($path, $name);
        $attModel->save();

        return $attModel;
        }
    public function saveFile($path = null, $name = null)
        {
        $file = request()->file;
        // Image::class;
        $path = $path == null ? $this->getPath() : $path;
        $name = $this->getName($file, $name);
        $path = "/" . $path . "/" . $name;

        $file->storeAs($path, $name);

        return $path;
        }


    public function autodelete($where = null)
        {
        return parent::autodelete($where)->filter(function ($v)
            {
            //此附件被使用，原始文件就不删除，没有别的单据使用，就删除原文件
            if ($this->whereUuid($v->uuid)->where("id", "<>", $v->id)->doesntExist()) {
                Storage::delete([$v->filepath, $v->filethumbnail]);
                }
            //原文件的附件标识置零
            $source_exist = $this
                ->whereSource_model($v->source_model)
                ->whereSource_key($v->source_key)
                ->whereSource_value($v->source_value)
                ->doesntExist();
            !$source_exist && $v->source?->update(['attachment' => 0]);
            });
        }
    protected function getPath()
        {
        return auth()->user()->employee_id . "/" . date("Ymd");
        }
    protected function getName($file, $name = null)
        {
        return $name == null ? (date("YmdHis") . rand(100, 999) . "." . $file->getClientOriginalExtension()) : $name;
        }
    protected function isImage($file)
        {
        return substr($file->getMimeType(), 0, 5) == 'image';
        }
    }